<template>
  <centre>
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" class="form">
      <h3>用户注册</h3>
      <el-form-item label="用户" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="form.checkPass"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('ruleForm')">注册</el-button>
        <el-button @click="onReset('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </centre>
</template>

<script>
import Centre from "../components/Centre";
import {register} from "@/services/userServices";

export default {
  name: "Register",
  components: {
    Centre
  },
  data() {
    let password = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    let checkPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致！'))
      } else {
        callback();
      }
    };
    return {
      form: {
        username: '132',
        password: '',
        checkPass: '',
        nickname: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: blur},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: blur},
          {validator: password, trigger: blur}
        ],
        checkPass: [
          {required: true, message: '请输入再次输入密码', trigger: blur},
          {validator: checkPass, trigger: blur}
        ]
      }
    }
  },
  methods: {
    onSubmit(form) {
      this.$refs[form].validate((res) => {
        if (res) {
          this.register().then((res) => {
            if (res.code === 1 ) {
              this.$message.success(res.msg);
            }else {
              this.$message.error(res.msg);
            }
          })

        } else {
          return false;
        }
      })
    },
    onReset(form) {
      this.$refs[form].resetFields();
    },
    async register() {
      let resp = await register(this.form);
      return resp;
    }
  }
}
</script>

<style scoped lang="scss">
.form {
  width: 340px;
  padding: 32px 48px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3);

  h3 {
    font-size: 1.7rem;
    text-align: center;
    margin-bottom: 32px;
    color: #303133;
  }
}
</style>
